#include "Renderer_PCH.h"
#include "RRasterizerState.h"

/*--------------------------------------------------------------------------*/

RRasterizerState::RRasterizerState() :
	m_frontFace(GL_CCW),
	m_cullFace(GL_FALSE),
	m_frontPolygonMode(GL_FILL),
	m_backPolygonMode(GL_FILL),
	m_colorMask(GL_TRUE)
{

}

/*--------------------------------------------------------------------------*/

GLenum RRasterizerState::GetFrontFace() const
{
	return m_frontFace;
}

/*--------------------------------------------------------------------------*/

GLenum RRasterizerState::GetCullFace() const
{
	return m_cullFace;
}

/*--------------------------------------------------------------------------*/

GLenum RRasterizerState::GetFrontPolygonMode() const
{
	return m_frontPolygonMode;
}

/*--------------------------------------------------------------------------*/

GLenum RRasterizerState::GetBackPolygonMode() const
{
	return m_frontPolygonMode;
}

/*--------------------------------------------------------------------------*/

GLenum RRasterizerState::GetColorMask() const
{
	return m_colorMask;
}

/*--------------------------------------------------------------------------*/

void RRasterizerState::SetFrontFace(GLenum n)
{
#ifdef _DEBUG
	__Check(n == GL_CCW || n == GL_CW);
#endif
	m_frontFace = n;
}

/*--------------------------------------------------------------------------*/

void RRasterizerState::SetCullFace(GLenum n)
{
#ifdef _DEBUG
	__Check(n == GL_FRONT || n == GL_BACK || n == GL_NONE || n == GL_FRONT_AND_BACK);
#endif
	m_cullFace = n;
}

/*--------------------------------------------------------------------------*/

void RRasterizerState::SetFrontPolygonMode(GLenum n)
{
#ifdef _DEBUG
	__Check(n == GL_FILL || n == GL_LINE ||	n == GL_POINT);
#endif
	m_frontPolygonMode = n;
}

/*--------------------------------------------------------------------------*/

void RRasterizerState::SetBackPolygonMode(GLenum n)
{
#ifdef _DEBUG
	__Check(n == GL_FILL || n == GL_LINE || n == GL_POINT);
#endif
	m_backPolygonMode = n;
}

/*--------------------------------------------------------------------------*/

void RRasterizerState::SetColorMask(GLenum n)
{
#ifdef _DEBUG
#endif
	m_colorMask = n;
}

/*--------------------------------------------------------------------------*/

void RRasterizerState::Bind() const
{
	glFrontFace(m_frontFace);

	if (m_cullFace == GL_NONE)
	{
		glDisable(GL_CULL_FACE);
	}
	else
	{
		glEnable(GL_CULL_FACE);
		glCullFace(m_cullFace);
	}

	if (m_frontPolygonMode != m_backPolygonMode)
	{
		glPolygonMode(GL_FRONT, m_frontPolygonMode);
		glPolygonMode(GL_BACK, m_backPolygonMode);
	}
	else
	{
		glPolygonMode(GL_FRONT_AND_BACK, m_frontPolygonMode);
	}

	glColorMask(m_colorMask, m_colorMask, m_colorMask, m_colorMask);
}

/*--------------------------------------------------------------------------*/